import React, { Component, Fragment } from "react";
// antd
import { Button } from "antd";
import Action from "@c/buttonAction/Index";
// table 组件
import TableComponent from "@c/tableData/Index";
import DutyModal from "./components/DutyModal";

class DutyList extends Component {
    constructor(props){
        super(props);
        this.state = {
            // id
            id: "",
            // flag
            flag: false,
            // 请求参数
            pageNumber: 1,
            pageSize: 10,
            keyWork: "",
            // 表头
            tableConfig: {
                url: "dutyList",
                method: "get",
                checkbox: true,
                thead: [
                    { 
                        title: "项目名", 
                        dataIndex: "project_code", 
                        key: "project_code"
                    },
                    { 
                        title: "值班运维", 
                        dataIndex: "sa", 
                        key: "sa",
                    },
                    { 
                        title: "号码", 
                        dataIndex: "tel", 
                        key: "tel",
                    },
                    { 
                        title: "优先级", 
                        dataIndex: "weight", 
                        key: "weight",
                    },
                    { 
                        title: "操作", 
                        dataIndex: "operation", 
                        key: "operation", 
                        width: 215,
                        render: (text, rowData) => {
                            return (
                                <div className="inline-button">
                                    <Action>
                                        <Action.Button onClick={() => this.userModal({status: true, duty_id: rowData.id})}>编辑</Action.Button>
                                        <Action.Button onClick={() => this.delete(rowData.id)}>删除</Action.Button>
                                    </Action>
                                    {/* 
                                        在父组件获取子组件的实例
                                        1、在子组件调用父组件方法，并把子组件实例传回给父组件，（已经存储了子组件的实例）
                                        2、通过实例调用子组件的方法
                                    */}
                                </div>
                            )
                        }
                    }
                ],
                formItem: [
                    { 
                        type: "Input",
                        label: "项目代号", 
                        name: "project_code", 
                    },
                ],
                formSearchCol: 18,
                formRightCol: 6,
            },
            // 表的数据
            data: []
        };
    }
    /** 生命周期挂载完成 */
    componentDidMount(){}

    // 获取子组件实例
    getChildRef = (ref) => {
        this.tableComponent = ref; // 存储子组件
    }
    // 获取弹窗子组件实例
    getUserModalRef = (ref) => { this.child = ref }
    // 显示弹窗
    userModal = (data) => {
        this.child.visibleModal(data);
    }

    /** 删除 */
    delete = (id) => {
        this.tableComponent.onHandlerDelete(id)
    }
    render(){
        return (
            <Fragment>
                <TableComponent onRef={this.getChildRef} batchButton={true} config={this.state.tableConfig}>
                    <Button type="primary" ref="dutyAdd" onClick={() => this.userModal({ status: true })}>添加</Button>
                </TableComponent>
                <DutyModal onRef={this.getUserModalRef} />
            </Fragment>
        )
    }
}

export default DutyList;